<?php
/**
 * Created by PhpStorm.
 * User: zd
 * Date: 2018/9/30
 * Time: 11:40
 */

namespace app\api\model;



class ThirdApp extends BaseModel
{
    protected  $createTime = false;

    /**
     * 与work表一对一关系
     * @return \think\model\relation\HasOne
     */
    public function work(){
        return $this->hasOne('Work','t_id','id');
    }

    /**
     * 与work_department表一对多关系
     * @return \think\model\relation\HasMany
     */
    public function department(){
        return $this->hasMany('WorkDepartment','t_id','id');
    }

    public function mini(){
        return $this->hasOne('Mini','t_id','id');
    }

    public function workuser(){
        return $this->hasMany('WorkUser','t_id','id');
    }

    public function worklist(){
        return $this->hasMany('WorkUser','t_id','id');
    }

    /**
     * 一对一关系
     * @return \think\model\relation
     */
    public function img(){
        return $this->belongsTo('Image','head_img','id');
    }

    /**
     * 一对一关系
     * @return \think\model\relation
     */
    public function back(){
        return $this->belongsTo('Image','index_img','id');
    }

    /**
     * 查询用户是否存在
     * @param $ac
     * @return array|false|\PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
        public static function check($ac){
            return self::where('app_id','=',$ac)->find();
    }

    /**
     * 获取名片信息
     * @param $t_id
     * @return array|false|\PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getAppInfo($t_id){
       $data  =  self::where('id','=',$t_id)
                ->with(['img','back'])
                ->field('name,sub_name,address,head_img,index_img')
                ->find();
       return $data;
    }

    /**
     * 员工数量数据
     * @param $id
     * @return array|false|\PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getWorkUserNum($id){
        return self::withCount(['workuser'=>function($query) use($id){
           $query->where('t_id','=',$id);
            }])
            ->find($id)
            ->visible(['workuser_count']);
    }

    /**
     * 获取已开名片数量
     * @param $id
     * @return array|false|\PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getCardNum($id){
        return self::withCount(['workuser'=>function($query) use($id){
            $query->where('t_id','=',$id)->where('ai_power','=','on');
        }]) ->find($id)
            ->visible(['workuser_count']);
    }

    /**
     * 获取workUser信息
     * @param $id
     * @return array|false|\PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getWorkUser($id){
       $data =  self::with(['workuser'])->where('id','=',$id)->find();
       return $data;
    }

    /**
     * 根据id获取用户信息
     * @param $uid
     * @return array|false|\PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getUserInfoByUserId($uid){
            return self::where('id','=',$uid)->find();
    }

    /**
     * 查询小程序授权信息
     * @param $uid
     * @return array|false|\PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getDetailUserInfoByMini($uid){
        return self::where('id','=',$uid)->with(['mini'])->cache(true,'7200')->find();
    }

    /**
     * 返回企业授权信息
     * @param $uid
     * @return array|false|\PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getUserAuthInfo($uid){
        return self::where('id','=',$uid)->with(['work'])->find();
    }

    public static function getUserInfo($uid){
        return self::where('id','=',$uid)
            ->with(['work','img'])
            ->with(['workuser'=>function($qury){
                $qury->where('ai_power','=','on');
            }])
            ->find();
    }
    /**
     * 更新用户信息
     * @param $uid
     * @param $data
     * @return $this
     */
    public static function updateUserInfo($uid,$data){
        return self::where('id','=',$uid)->update($data);
    }

    /**
     * 返回用户部门信息
     * @param $uid
     * @return array|false|\PDOStatement|string|\think\Model
     */
    public static function getUserDepartmentInfo($uid){
        return self::where('id','=',$uid)
            ->with(['department'])->find();
    }

    /**
     * 新增部门，关联新增
     * @param $uid
     * @param $department
     * @return mixed
     */
    public static function addUserDepartment($uid,$department){
        $User =  self::where('id','=',$uid)->find();
        $result = $User->department()->save($department);
        return $result;
    }

    /**
     * 批量新增
     * @param $uid
     * @param $department
     * @return mixed
     */
    public static function addUserAllDepartment($uid,$department){
        $User =  self::where('id','=',$uid)->find();
        $result = $User->department()->saveAll($department);
        return $result;
    }

    /**
     * 获取所有名片数量
     * @param $t_id
     * @param $time
     * @return int|string
     * @throws \think\Exception
     */
    public static function getAllUser($t_id,$time){
        $num = User::where('t_id','=',$t_id)
                ->where('nickname is not null')
                ->where('create_time','>',$time)->count();
        return $num;
    }

    /**
     * 获取名片开通数量
     * @param $t_id
     * @return array|false|\PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getApplyUser($t_id){
        $num =  self::where('id','=',$t_id)
                ->withCount(['workuser'=>function($query){
                    $query->where('ai_power','=','on');
                }])
                ->withCount(['worklist'=>function($query){
                    $query->where('boss_power','=','on');
                }])
                ->find()
                ->hidden(['web_info']);
        return $num;
    }

    /**
     * 每日新增用户数
     * @param $t_id
     * @param $time
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function  getAllUserByTime($t_id,$time){
        $info = self::with(['workuser'])->where('id','=',$t_id)->field('id')->find();
        foreach ($info->workuser as $value){
            $wu_id[] = $value->id;
        }
        $data = UserWorkuser::Where('wu_id','in',$wu_id)
            ->where('create_time',">=",$time)
            ->field("FROM_UNIXTIME(create_time,'%m-%d') days,count(id) num")
            ->group('days')
            ->select();
        return $data;
    }

    /**
     * 获取中跟进数
     * @param $t_id
     * @return int|string
     */
    public static function getAllFollowUser($t_id,$time){
        $info = self::with(['workuser'])->where('id','=',$t_id)->field('id')->find();
        foreach ($info->workuser as $value){
            $wu_id[] = $value->id;
        }
        $num =  Follow::getAllNum($wu_id,$time);
        return $num;
    }

    /**
     * 每日跟进数
     * @param $t_id
     * @param $time
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getAllFollowUserByTime($t_id,$time){
        $info = self::with(['workuser'])->where('id','=',$t_id)->field('id')->find();
        foreach ($info->workuser as $value){
            $wu_id[] = $value->id;
        }
        $num = Follow::getAllFollowList($wu_id,$time);
        return $num;
    }

    /**
     * 获取管理列表
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getThirdList(){
        return self::where('mini_id != 0')
            ->where('w_id != 0')
            ->field('id')->select();
    }


}